<?php

	function treecategories_init(){
		global $CONFIG;
		// Registrar los ficheros de idiomas
		register_translations($CONFIG->pluginspath . "treecategories/languages/");
		
		register_plugin_hook("action", "blog/add", "treecategories_action_hook");
	  register_plugin_hook("action", "blog/edit", "treecategories_action_hook");

		//Extend css
		extend_view('css', 'treecategories/css');
		if(get_context()=='blog')
			extend_view('input/longtext','treecategories/dropdown');
	}
	
	function treecategories_action_hook($hook, $entity_type, $returnvalue, $params) {
		$category = get_input('category');
		if (!empty($category))
			return true;
		else {
			// don't let the fields get empty
			$title = get_input('blogtitle');
			$body = get_input('blogbody');
			$tags = get_input('blogtags');

			// Cache to the session
			$_SESSION['user']->blogtitle = $title;
			$_SESSION['user']->blogbody = $body;
			$_SESSION['user']->blogtags = $tags;

			register_error(elgg_echo("blog:category:blank"));
			if ($entity_type == 'blog/edit')
				forward("mod/blog/edit.php?blogpost=" . get_input('blogpost'));
			else
				forward("mod/blog/add.php");
		}
	}

	/**
  * Save category
  *
 */
	function treecategories_save($event, $object_type, $object) {
		if ($object->getSubtype() == 'blog') {
			$cat = get_input('category');
			$object->category = (string) $cat;
		}
		return true;
	}

	global $CONFIG;
	
	// Events
	register_elgg_event_handler('init','system','treecategories_init');
	register_elgg_event_handler('update','all','treecategories_save');
	register_elgg_event_handler('create','all','treecategories_save');
?>